package com.analyse.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.analyse.stock.constant.ApiConstant;
import com.analyse.stock.constant.StockConstant;
import com.analyse.stock.mapper.StockKlineYearlyMapper;
import com.analyse.stock.model.StockInfoModel;
import com.analyse.stock.model.StockKlineYearlyModel;
import com.analyse.stock.service.IStockKlineYearly;
import com.analyse.stock.utils.HttpUtil;
import com.analyse.stock.utils.StockStringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class StockKlineYearlyService extends ServiceImpl<StockKlineYearlyMapper, StockKlineYearlyModel> implements IStockKlineYearly {

    @Override
    public Integer getStockKlineYearlyHistoryByEastMoney(List<StockInfoModel> stockList) {
        if(CollUtil.isEmpty(stockList)){
            return 0;
        }
        String klineType = "104";
        List<Integer> years = StockStringUtil.getAllYear(ApiConstant.BEGIN_DATE);
        AtomicInteger total = new AtomicInteger(0);
        for(StockInfoModel model : stockList){
            for(Integer year : years){
                long time = System.currentTimeMillis()*1000;
                String startDate = String.format("%s0101",year);
                String endDate = String.format("%s1231",year);
                String url = StrUtil.format(ApiConstant.KLINE_URL,
                        startDate,endDate,String.format("%s.%s",model.getMarkCode(),model.getStockCode()),klineType,time);
                String result = HttpUtil.sendGet(url,"*/*", StockConstant.UTF8,6000L);
                if(StringUtils.isBlank(result)){
                    continue;
                }
                JSONObject object = JSON.parseObject(result);
                if(Objects.isNull(object) || Objects.isNull(object.getJSONObject("data")) ||
                        StringUtils.isBlank(object.getJSONObject("data").getString("klines"))){
                    continue;
                }
                JSONArray array = JSON.parseArray(object.getJSONObject("data").getString("klines"));
                if(Objects.isNull(array) || array.size()<1){
                    log.info("## 股票:{}, {}没有:{}-{} 的年K数据",model.getStockCode(),model.getStockName(),startDate,endDate);
                    continue;
                }
                log.info("## 股票:{}, {} 开始解析:{}-{} 的年K数据",model.getStockCode(),model.getStockName(),startDate,endDate);
                List<StockKlineYearlyModel> modes = Lists.newArrayList();
                for(Object vo : array){
                    String[] str = vo.toString().split(StrUtil.COMMA);
                    StockKlineYearlyModel monthlyModel = StockKlineYearlyModel.transStockKlineModel(str,model.getStockCode());
                    if(Objects.nonNull(monthlyModel)){
                        modes.add(monthlyModel);
                    }
                }
                super.saveBatch(modes);
                total.getAndAdd(modes.size());
            }
        }
        return total.get();
    }
}
